package com.hongye.broker.exception;

import com.hongye.common.exception.ServiceException;
import com.hongye.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

/*
    凤兮凤兮归故乡，遨游千里求其凰
    2021/7/16
*/
@Slf4j
@RestControllerAdvice
public class GlobalException {

    //具体拦截哪一类异常
    @ExceptionHandler(Exception.class)
    public ResponseResult exceptionHandler(Exception exception, HttpServletResponse response){
        if (exception instanceof ServiceException){
            String code = ((ServiceException) exception).getCode();
            String msg = ((ServiceException) exception).getMsg();
            return ResponseResult.error(Integer.valueOf(code),msg);
        }
        //异常信息
        log.error(exception.getMessage());
        return ResponseResult.error(404,"系统异常");
    }

}
